<?php

namespace App\Api\Controllers;

use App\Models\Commodity;
use App\Models\Order;
use App\Models\OrderGood;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrdersController extends Controller
{

    public function store(Request $request){
        $products = $request->input('products');
        $orderAddress = $request->input('order_address');
        $products = json_decode($products,true);
        //检查商品
        $check = $this->checkGood($products);
        if(!is_numeric($check)) {
            return $this->error($check);
        }
//        //判断用户余额
//        if($this->user()->balance<$order->total_price){
//            return $this->error('剩余余额不足，请先充值');
//        }

        DB::beginTransaction();
        //生成订单
        $order = $this->createOrder($check,$orderAddress);
        //生成子订单
        $this->createOrderGood($order->id,$products);
        DB::commit();
        //可能会有分销
        return $this->success($order);
    }

    public function createOrder($totalPrice,$orderAddress){
        $order = new Order();
        $order->orders_no = $this->makeOrderNo();

        $order->user_id = $this->user()->id;
        $order->order_address = $orderAddress;
        $order->total_price = $totalPrice;
        $order->save();
        return $order;
    }

    public function createOrderGood($order_id,$products){
        $commodity = new Commodity();
        foreach($products as $product){
            $machines = $commodity->where('status',1)->find($product['good_id']);
            $orderGood = new OrderGood();
            $orderGood->user_id = $this->user()->id;
            $orderGood->order_id = $order_id;
            $orderGood->good_name = $machines->name;
            $orderGood->price = $machines->price;
            $orderGood->num = $product['num'];
            $orderGood->good_img = $machines->img;
            $orderGood->save();
        }
        return $orderGood;
    }

    public function checkGood($products){
        $commodity = new Commodity();
        $totalPrice = 0;
        foreach($products as $product) {
            $machines = $commodity->where('status',1)->find($product['good_id']);
            if (!$machines) {
                return '商品不存在';
            }
            //判断矿机剩余算力
            if($machines->stock<$product['num']){
                return '库存不足';
            }
            $totalPrice+=$machines['price']*$product['num'];
        }
        return $totalPrice;
    }

    //生成订单号
    public function makeOrderNo()
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn =
            $yCode[intval(date('Y')) - 2017] . strtoupper(dechex(date('m'))) . date(
                'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
                '%02d', rand(0, 99));
        return $orderSn;
    }

    public function index(){
        return $this->success($this->user()->order()->with('machine')->with('orderGood')->orderBy('id','desc')->get());
    }
}
